package com.etime.shycourse.controller.orders;


import com.etime.shy.common.BaseMessage;
import com.etime.shy.common.ListMessage;
import com.etime.shy.common.PojoMessage;
import com.etime.shycourse.orde.dto.BuyCarIncludeGoodsDto;
import com.etime.shycourse.orde.pojo.Buycar;
import com.etime.shycourse.orde.services.interfaces.BuycarServices;
import com.etime.shycourse.wx.applets.dto.UserTeacherDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author wb
 * @since 2022-01-12
 */
@Api(tags = "微信购物车数据操作接口")
@RestController
@RequestMapping("/wx/buyCar")
public class WXBuycarController {
    @Autowired
    BuycarServices services;

    /**
     * 加入购物车数据
     * insetOrUpdateBuyCar
     *
     * @param buyCarList
     * @return int 影响数据的条数 0 失败 1成功
     * int insetOrUpdateBuyCar(Buycar buycar);
     */
    @ApiOperation("创建购物车")
    @PostMapping("/insert")
    public BaseMessage insetOrUpdateBuyCar(@RequestBody List<Buycar> buyCarList, HttpServletRequest request) {
        BaseMessage msg = new BaseMessage ();
        String token = request.getHeader ("token");
        if (token == null || token.equals ("")) {
            msg.setCode (-2);
            msg.setMsg ("未获取到用户信息；可能登录超时");
            return msg;
        }
        int rtn = services.insetOrUpdateBuyCar (buyCarList, token);
        msg.setCode (rtn);
        if (rtn > 0) {
            msg.setMsg ("创建购物车成功");
        } else {
            msg.setMsg ("创建购物车失败");
        }
        return msg;
    }

    /**
     * 查询购物车数据接口 buycaruserid 用户id 改为token
     * 改为token   2022-02-12 14:17:55
     *
     * @param
     * @return BuyCarIncludeGoodsTypeDto 购物车包括 课程信息 Dto
     * BuyCarIncludeGoodsTypeDto selectBuyCarIncludeGoodsTypeByUserId(Integer buyCarUserId);
     */
    @ApiOperation("查询购物车数据接口 购物车+课程信息")
    @GetMapping("/get/carIncludeGoods")
    public ListMessage<BuyCarIncludeGoodsDto> selectBuyCarIncludeGoodsByUserId(HttpServletRequest request) {
        ListMessage<BuyCarIncludeGoodsDto> msg = new ListMessage<> ();
        String token = request.getHeader ("token");
        if (token == null || token.equals ("")) {
            msg.setCode (-2);
            msg.setMsg ("未获取到用户信息；可能登录超时");
            return msg;
        }
        List<BuyCarIncludeGoodsDto> data = services.selectBuyCarIncludeGoodsByUserIds (token);
        if (data != null) {
            msg.setCode (1);
            msg.setDatalist (data);
        } else {
            msg.setCode (0);
            msg.setMsg ("没有查询到购物车数据");
        }
        return msg;
    }

    /**
     * deleteByUserId
     * 删除购物车中的信息 物理删除
     *
     * @param buyCarUserId 用户id
     * @return int 影响数据的条数 0 失败 1成功
     * int deleteByUserId(Integer buyCarUserId);
     */
    @ApiOperation("根据buyCarUserId 删除购物车中的信息 物理删除 清空购物车")
    @PostMapping("/delete/empty/{buyCarUserId}")
    public BaseMessage deleteByUserId(@PathVariable Integer buyCarUserId) {
        int rtn = services.deleteByUserId (buyCarUserId);
        BaseMessage msg = new BaseMessage ();
        msg.setCode (rtn);
        if (rtn > 0) {
            msg.setMsg ("清空购物车中的信息成功");
        } else {
            msg.setMsg ("清空购物车中的信息失败");
        }
        return msg;
    }

    /**
     * 根据购物车id 查询购物车一条数据
     * 购物车的详细信息 课程 + 章节 + 课时
     * selectBuyCarById
     *
     * @param buyCarId
     * @return Buycar
     */
    @ApiOperation("根据购物车id(buyCarId)-> 查询购物车一条数据 详细信息 课程 + 章节 + 课时")
    @GetMapping("/get/buyCar/{buyCarId}")
    public PojoMessage<BuyCarIncludeGoodsDto> selectBuyCarById(@PathVariable Integer buyCarId) {
        PojoMessage<BuyCarIncludeGoodsDto> msg = new PojoMessage<> ();
        BuyCarIncludeGoodsDto data = services.selectBuyCarById (buyCarId);
        if (data != null) {
            msg.setCode (1);
            msg.setData (data);
        } else {
            msg.setCode (0);
            msg.setMsg ("没有找到购物车数据");
        }
        return msg;
    }

    /**
     * deleteByBuyCarId
     * 删除购物车中的信息 物理删除
     *
     * @param buyCarId 购物车id
     * @return int 影响数据的条数 0 失败 1成功
     */
    @ApiOperation("根据购物车id 删除购物车一条信息 物理删除")
    @PostMapping("/delete/buyCar/{buyCarId}")
    public BaseMessage deleteByBuyCarId(@PathVariable Integer buyCarId) {
        int rtn = services.deleteByBuyCarId (buyCarId);
        BaseMessage msg = new BaseMessage ();
        msg.setCode (rtn);
        if (rtn > 0) {
            msg.setMsg ("删除购物车中的信息成功");
        } else {
            msg.setMsg ("删除购物车中的信息失败");
        }
        return msg;
    }

    /**
     * iFBuyCarCourse
     * 根据用户id 课程 判断是否加入购物车
     *
     * @param courseID 课程id
     * @return int  存在返回 1 不存在返回 0
     */
    @ApiOperation("根据 课程id 判断是否加入购物车")
    @GetMapping("/if/buyCar/{courseID}")
    public BaseMessage iFBuyCarCourse(@PathVariable Integer courseID, HttpServletRequest request) {
        BaseMessage msg = new BaseMessage ();
        String token = request.getHeader ("token");
        if (token == null || token.equals ("")) {
            msg.setCode (-2);
            msg.setMsg ("未获取到用户信息；可能登录超时");
            return msg;
        }
        Integer rtn = services.iFBuyCarCourse (courseID, token);
        msg.setCode (rtn);
        if (rtn == 1) {
            msg.setMsg ("已加入购物车");
        } else {
            msg.setMsg ("未加入购物车");
        }
        return msg;
    }

}

